package com.yugu.park.security.handle;

import java.io.IOException;
import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSON;
import com.yugu.enums.HttpCode;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
	
	private static final long serialVersionUID = -8970718410437077606L;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
            throws IOException
    {
        HttpCode code = HttpCode.UNAUTHORIZED;
        String msg = StrUtil.format("请求访问：{}，认证失败，无法访问系统资源", request.getRequestURI());
        ModelMap map = new ModelMap();
        map.put("httpCode", code.value());
        map.put("msg", msg);
        ServletUtil.write(response, JSON.toJSONString(map), "application/json;charset=UTF-8");
    }
    
}
